//
//  HHHPublicClass.m
//  project
//
//  Created by 黄建华 on 2017/9/27.
//  Copyright © 2017年 黄建华. All rights reserved.
//

#import "SNPublicClass.h"
@implementation SNPublicClass

DEF_SINGLETON(SNPublicClass)

- (instancetype)init {
    self = [super init];
    if (self) {
        _selectDate   = [NSDate date];
        _stepArray    = [NSMutableArray array];
        _heartArray   = [NSMutableArray array];
        _oxArray      = [NSMutableArray array];
        _bpMaxArray   = [NSMutableArray array];
        _bpMinArray   = [NSMutableArray array];
        _trackArray   = [NSMutableArray array];
        _checkType    = 1;
        _isCheck      = NO;
        _isFriendNewMessage = NO;
        [BackGroundTaskView sharedInstance];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(BecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(SNPublicClassTimer) userInfo:nil repeats:YES];
        
        [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
        _userModel      = [SNEveryDayModelHelp getUserModel];
        _lastDateString = [self showDateWithType:0];
//        _weatherModel = [WeatherModel getModelFromDBWithKey:@"0"];
//        [self performSelector:@selector(updateWeather) withObject:nil afterDelay:0.05];
        _isMainEdit = NO;
    }
    return self;
}

- (void)updateWeather
{
//NSDictionary *dic = [SAVEWEATHERDIC getObjectValue];
    NSArray *array = [SNWEATHERSAVE getObjectValue];
    for (int k = 0; k <array.count; k ++) {
        NSDictionary *dic = [array objectAtIndex:k];
        SNWeatherModel *model = [SN_USER.weatherArray objectAtIndex:k];
        
        NSString *weatherInfo = dic[@"cond_txt"];
        NSString *maxTem = dic[@"tmp_max"];
        NSString *minTem = dic[@"tmp_min"];
        NSString *weatherCode = dic[@"cond_code"];
        NSString *tem = dic[@"tmp"];
        if (![weatherInfo isEqual:[NSNull null]]) {
            model.weatherInfo = weatherInfo;
        }
        if (![maxTem isEqual:[NSNull null]]) {
            model.maxTem = maxTem;
        }
        if (![minTem isEqual:[NSNull null]]) {
            model.minTem = minTem;
        }
        if (![weatherCode isEqual:[NSNull null]]) {
            model.weatherCode = weatherCode;
        }
        
        if (![tem isEqual:[NSNull null]]) {
            model.tem = tem;
        }
        
        if (k == 0) {
            _weatherModel = [WeatherModel getModelFromDBWithKey:@"0"];
            if (![weatherInfo isEqual:[NSNull null]]) {
                _weatherModel.weatherInfo = weatherInfo;
            }
            if (![maxTem isEqual:[NSNull null]]) {
               _weatherModel.maxTem = maxTem;
            }
            if (![minTem isEqual:[NSNull null]]) {
                _weatherModel.minTem = minTem;
            }
            if (![weatherCode isEqual:[NSNull null]]) {
                _weatherModel.weatherCode = weatherCode;
            }
            
            if (![tem isEqual:[NSNull null]]) {
                _weatherModel.tem = tem;
            }
            [_weatherModel updateToDB];
        }
    }
    [SN_USER updateToDB];
}

- (void)updateUser {
    NSLog(@"登录成功>>>>>>>>%@ >>>>>%@", SN_USER.user_id, [KK_LastUser getObjectValue]);
    _userModel = [SNEveryDayModelHelp getUserModel];
    
    [SNFoodClass sharedInstance];
}

- (void)SNPublicClassTimer {
//    _count += 10;
//    if (_count >= 300) {
//        if (!KK_BLESend.isSend && !_isCheck && !KK_BLEConnect.isUpdateing && [HASLOGIN getBOOLValue]) {
//            NSLog(@"定时器5分钟");
////            [KK_BLESend sendCMD];
//            if (ISOPENFRIENDFUNCTION) {
//                [SN_Net friendSystemNoReadSuccess:^(id object) {
//                    
//                } fail:^(id object) {
//                    
//                }];
//            }
////            NSLog(@"同步运动睡眠心率大数据>>>>>>>>>");
////            NSString *log = [NSString stringWithFormat:@"自动同步心率运动睡眠数据"];
////            [self writefileWith:log];
//            _count = 0;
//        }
////        if ([HASLOGIN getBOOLValue]) {
////            if ([[UIDevice currentDevice]systemVersion].floatValue >= 9.0) {
////                [SN_HealthKit sendAllData];
////            }
////        }
//    }
    
    _reloadSportDataCount += 10;
    if (_reloadSportDataCount >= 120) {
        if (!KK_BLESend.isSend && !_isCheck && !KK_BLEConnect.isUpdateing && [HASLOGIN getBOOLValue]) {
            NSLog(@"定时器2分钟");
            [KK_BLESend sendCMD];
            _reloadSportDataCount = 0;
        }
    }
}

#pragma mark --- 应用切换到前台模式 ---
- (void)BecomeActive:(NSNotification *)Info {
    if ([_lastDateString isEqualToString:SN_Text(@"今天")]) {
        SN_PublicClass.selectDate = [NSDate date];
    }
//    if ([HASLOGIN getBOOLValue]) {
//        if ([[UIDevice currentDevice]systemVersion].floatValue >= 9.0) {
//            [SN_HealthKit sendAllData];
//        }
//    }
//    if (!KK_BLEConnect.centerPowerOn && [HASLOGIN getBOOLValue]) {
//        SHOWMBProgressHUD(nil, SN_Text(@"检测到你的手机蓝牙功能未启用，请前往手机的[设置]启用蓝牙功能"), nil, NO, 2.0);
//    }
    
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"activeTime"]) {
        NSInteger second = [NSDate intervalSecondsWithSmallDate:[[NSUserDefaults standardUserDefaults] objectForKey:@"activeTime"] bigDate:[NSDate date]];
        if (second >= 1800) {
//            [kK_App latestRealSportDataNotification:@"间隔30分钟同步大数据" subtitle:@"" identifier:@"SynchronousBigDatas"];
            dispatch_async(dispatch_get_main_queue(), ^{
                [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"activeTime"];
                [KK_BLESend sendCMD];
//                [KK_BLESend updateData];
            });
        }
    } else {
        [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"activeTime"];
    }
//    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(delayUpdateData) object:nil];
//    [self performSelector:@selector(delayUpdateData) withObject:nil afterDelay:30.0];
}

//- (void)delayUpdateData
//{
//    if (!KK_BLESend.isSend && KK_BLEModel.isConnected && SN_USER.autoUpdateData)
//    {
//        NSLog(@"进入前台刷新大数据>>>>>");
//
//        [KK_BLESend updateData];
//    }
//}

- (void)setSelectDate:(NSDate *)selectDate {
    _selectDate     = selectDate;
    _lastDateString = [self showDateWithType:0];
}

//******************* 主页面UI显示属性 begin*******************
- (NSString *)showSelectDate {
    if ([[_selectDate dateToDayString] isEqualToString:[[NSDate date]dateToDayString]]) {
        return SN_Text(@"今天");
    }
    return [_selectDate dateToDayString];
}

// 获取某日期所在的周月年 开始的日期 // 周月年 1 周 2 月 3年
- (NSDate *)getMonthBeginAndEndWith:(NSDate *)newDate withType:(NSInteger)type {
    if (newDate == nil) {
        newDate = [NSDate date];
    }
    NSDate *beginDate = nil;
    if (type == 1) {
        beginDate = [newDate dateByAddingDays:-newDate.weekday +1];
    } else if (type == 2) {
        beginDate = [newDate dateByAddingDays:-newDate.day +1];
    } else if (type == 3) {
        beginDate = [newDate dateByAddingDays:-newDate.dayOfYear +1];
    }
    return beginDate;
}

//返回开始日期 和 结束日期  获取某日期所在的周月年 开始的日期 // 周月年 1 周 2 月 3年
- (NSArray *)getBeginDateStringAndEndWith:(NSDate *)newDate withType:(NSInteger)type {
    if (newDate == nil) {
        newDate = [NSDate date];
    }
    NSDate *beginDate = nil;
    NSDate *endDate   = nil;
    if (type == 1) {
        beginDate = [newDate dateByAddingDays:-newDate.weekday  +1];
        endDate   = [newDate dateByAddingDays:-newDate.weekday  +7];
    } else if (type == 2) {
        beginDate = [newDate dateByAddingDays:-newDate.day +1];
        endDate   = [newDate dateByAddingDays:-newDate.day + newDate.daysInMonth];
    } else if (type == 3) {
        beginDate = [newDate dateByAddingDays:-newDate.dayOfYear +1];
        endDate   = [newDate dateByAddingDays:-newDate.dayOfYear + newDate.daysInYear];
    }
    NSMutableArray *array = [NSMutableArray array];
    [array addObject:[beginDate dateToDayString]];
    
    NSString *endString = [self endStringWith:endDate];
    [array addObject:endString];
    //    [array addObject:[endDate dateToDayString]];
    return array;
}

- (NSString *)endStringWith:(NSDate *)date {
    NSString  *select  = [date dateToDayString];
    NSInteger nowCount = [NSDate compareDate:[NSDate date] withAnotherDay:date];
    
    if (nowCount == 1) {
        select = [[NSDate date] dateToDayString];
    }
    return select;
}

//周 月 年显示日期
- (NSString *)showDateWithType:(SelectDateType)type {
    NSString *dateString = @"";
    
    if (type == 0) {
        if ([[_selectDate dateToDayString] isEqualToString:[[NSDate date]dateToDayString]]) {
            dateString = SN_Text(@"今天");
        } else {
            dateString = [_selectDate dateToDayString];
        }
    } else {
        NSDate *beginDate = nil;
        NSDate *endDate   = nil;
        NSDate *newDate   = _selectDate;
        if (type == 1) {
            beginDate = [newDate dateByAddingDays:-newDate.weekday  +1];
            endDate   = [newDate dateByAddingDays:-newDate.weekday  +7];
        } else if (type == 2) {
            beginDate = [newDate dateByAddingDays:-newDate.day +1];
            endDate   = [newDate dateByAddingDays:-newDate.day + newDate.daysInMonth];
        } else if (type == 3) {
            beginDate = [newDate dateByAddingDays:-newDate.dayOfYear +1];
            endDate   = [newDate dateByAddingDays:-newDate.dayOfYear + newDate.daysInYear];
        } else if (type == 4) {
            beginDate = _selectDate;
            endDate   = [newDate dateBySubtractingMonths:2];
        } else {
            
        }
        
        if (type == 1) {
            dateString = [NSString stringWithFormat:@"%@-%@", [beginDate dateToSelectDateShow], [endDate dateToSelectDateShow]];
        } else if (type == 2) {
            dateString = [NSString stringWithFormat:@"%@-%@", [beginDate dateToSelectDateShow], [endDate dateToSelectDateShow]];
        } else if (type == 3) {
            dateString = [NSString stringWithFormat:@"%ld/01-%ld/12", (long)_selectDate.year, (long)_selectDate.year];
        } else if (type == 4) {
            NSString *beginMonth = @"";
            if (_selectDate.month < 10) {
                beginMonth = [NSString stringWithFormat:@"0%ld",(long)_selectDate.month];
            } else {
                beginMonth = [NSString stringWithFormat:@"%ld",(long)_selectDate.month];
            }
            NSString *endMonth = @"";
            if (endDate.month < 10) {
                endMonth = [NSString stringWithFormat:@"0%ld",(long)endDate.month];
            } else {
                endMonth = [NSString stringWithFormat:@"%ld",(long)endDate.month];
            }
            
            dateString = [NSString stringWithFormat:@"%ld/%@-%ld/%@", (long)endDate.year,endMonth, (long)_selectDate.year,beginMonth];
        } else {
            
        }
    }
    return dateString;
}

// 设置多段字体 参数1 字号  参数2 改变文字2 参数3 改变文字2
- (NSMutableAttributedString *)label1With:(NSString *)text withFontsize:(CGFloat)fontsize withChangeText1:(NSString *)text1 withChangeText2:(NSString *)text2 {
    NSMutableAttributedString *noteStr ;
    if (!text) {
        return noteStr;
    }
    noteStr = [[NSMutableAttributedString alloc] initWithString:text];
    if (text1.length > 0) {
        NSUInteger first1 = [[noteStr string] rangeOfString:text1].location;
        // 需要改变的区间
        NSRange range1 = NSMakeRange(first1, text1.length);
        [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Regular" size:fontsize] range:range1];
    }
    
    if (text2.length > 0) {
        NSUInteger first2 = [[noteStr string] rangeOfString:text2].location;
        // 需要改变的区间
        NSRange range2 = NSMakeRange(first2, text2.length);
        [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Regular" size:fontsize] range:range2];
    }
    
    return noteStr;
}

- (NSMutableAttributedString *)labelSelfText:(NSString *)text withChangeText:(NSString*)text1 withFontsize:(CGFloat)fontSize withChangeColor:(UIColor *)color
{
    NSMutableAttributedString *noteStr ;
    if (!text) {
        return noteStr;
    }
    noteStr = [[NSMutableAttributedString alloc] initWithString:text];
    if (text1.length > 0) {
        NSUInteger first1 = [[noteStr string] rangeOfString:text1].location;
        // 需要改变的区间
        NSRange range = NSMakeRange(first1, text1.length);
        [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:fontSize] range:range];
        [noteStr addAttribute:NSForegroundColorAttributeName value: color range:range];
    }
    return noteStr;
}

// 设置多段字体 参数1 字号  参数2 改变文字2 参数3 改变文字2
- (NSMutableAttributedString *)attributed:(NSString *)text withFontsize:(CGFloat)fontsize withChangeText1:(NSString *)text1 {
    NSMutableAttributedString *noteStr ;
    if (!text) {
        return noteStr;
    }
    noteStr = [[NSMutableAttributedString alloc] initWithString:text];
    if (text1.length > 0) {
        // 需要改变的区间
        NSRange range1 = NSMakeRange(0, text.length - text1.length);
        [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Verdana-Bold" size:fontsize] range:range1];
    }
    return noteStr;
}

- (NSMutableAttributedString *)setChartViewCenterText:(NSString *)text allTextFontSize:(NSInteger)aSize AttributesTextFontSize:(NSInteger)size  {
    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
    paragraphStyle.alignment = NSTextAlignmentCenter;
    NSString *totalTime = text;
    
    NSMutableAttributedString *centerText = [[NSMutableAttributedString alloc] initWithString:totalTime];
    [centerText setAttributes:@{
                                NSFontAttributeName: [UIFont fontWithName:@"Verdana" size:size],
                                NSParagraphStyleAttributeName: paragraphStyle
                                } range:[totalTime rangeOfString:@"h"]];
    [centerText addAttributes:@{
                                NSFontAttributeName: [UIFont fontWithName:@"Verdana" size:size],
                                NSParagraphStyleAttributeName: paragraphStyle
                                } range:[totalTime rangeOfString:@"mins"]];
    NSRange hRange = [totalTime rangeOfString:@"h"];
    if ([text contains:@"\n"]) {
        [centerText addAttributes:@{
                                    NSFontAttributeName: [UIFont fontWithName:@"Verdana-Bold" size:aSize],
                                    NSForegroundColorAttributeName: [UIColor blackColor]
                                    } range:NSMakeRange(0, hRange.location)];
        
        [centerText addAttributes:@{
                                    NSFontAttributeName: [UIFont fontWithName:@"Verdana-Bold" size:aSize],
                                    NSForegroundColorAttributeName: [UIColor blackColor]
                                    } range:NSMakeRange(hRange.location + hRange.length+1, 2)];
    } else {
        [centerText addAttributes:@{
                                    NSFontAttributeName: [UIFont fontWithName:@"Verdana-Bold" size:aSize],
                                    NSForegroundColorAttributeName: [UIColor blackColor]
                                    } range:NSMakeRange(0, hRange.location)];
        
        [centerText addAttributes:@{
                                    NSFontAttributeName: [UIFont fontWithName:@"Verdana-Bold" size:aSize],
                                    NSForegroundColorAttributeName: [UIColor blackColor]
                                    } range:NSMakeRange(hRange.location + hRange.length, 2)];
    }
    return centerText;
}

- (NSString *)timeFormatted:(int)totalSeconds{
    int minutes = (totalSeconds / 60) % 60;
    int hours = totalSeconds / 3600;
    NSString *time = @"";
    if (hours == 0) {
        time = [NSString stringWithFormat:@"%d%@", minutes,@"mins"];
    } else {
        time = [NSString stringWithFormat:@"%02d%@\n%02d%@",hours,@"h", minutes,@"mins"];
    }
    return time;
}


- (void)writefileWith:(NSString *)string {
    return;
    NSArray  *paths    = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *homePath = [paths objectAtIndex:0];
    NSString *fileName = [NSString stringWithFormat:@"%@_updateDetail.text", [[NSDate date]dateToDayString]];
    //    NSString *fileName = [NSString stringWithFormat:@"sport_step_net.text"];
    NSString      *filePath    = [homePath stringByAppendingPathComponent:fileName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if (![fileManager fileExistsAtPath:filePath]) { //如果不存在
        NSString *str = [NSString stringWithFormat:@"日期:%@", [[NSDate date]dateToDayString]];
        [str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    }
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
    [fileHandle seekToEndOfFile];  //将节点跳到文件的末尾
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm:ss"];
    NSString *datestr    = [dateFormatter stringFromDate:[NSDate date]];
    NSString *str        = [NSString stringWithFormat:@"\n%@\n%@\n", datestr, string];
    NSData   *stringData = [str dataUsingEncoding:NSUTF8StringEncoding];
    [fileHandle writeData:stringData]; //追加写入数据
    [fileHandle closeFile];
}

- (void)writefileWithNet:(NSString *)string {
    return;
    NSArray  *paths    = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *homePath = [paths objectAtIndex:0];
    NSString *fileName = [NSString stringWithFormat:@"%@_uoloadToServer.text", [[NSDate date]dateToDayString]];
    //    NSString *fileName = [NSString stringWithFormat:@"sport_step_net.text"];
    NSString      *filePath    = [homePath stringByAppendingPathComponent:fileName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if (![fileManager fileExistsAtPath:filePath]) { //如果不存在
        NSString *str = [NSString stringWithFormat:@"日期:%@", [[NSDate date]dateToDayString]];
        [str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    }
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
    [fileHandle seekToEndOfFile];  //将节点跳到文件的末尾
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm:ss"];
    NSString *datestr    = [dateFormatter stringFromDate:[NSDate date]];
    NSString *str        = [NSString stringWithFormat:@"\n%@\n%@\n", datestr, string];
    NSData   *stringData = [str dataUsingEncoding:NSUTF8StringEncoding];
    [fileHandle writeData:stringData]; //追加写入数据
    [fileHandle closeFile];
}


-(SortModel*) friendSortModel
{
    NSString *dateStr = [[NSDate date] dateToDayString];
    if (!_friendSortModel || ![_friendSortModel.dateString isEqualToString:dateStr]) {
        _friendSortModel = [SortModel getSortModelFromDBWithDayString:dateStr];
    }
    return _friendSortModel;
}

- (FoodModel *)todayFood
{
    NSString *dateStr = [[NSDate date] dateToDayString];
    if (!_todayFood || ![_todayFood.dateString isEqualToString:dateStr]) {
        _todayFood = [FoodModel getFoodModelFromDBWithDayString:dateStr];
    }
    return _todayFood;
}

#pragma sportModel
// 今日与昨天的运动参数
- (SportModel *)todaySport {
    NSString *dateStr = [[NSDate date] dateToDayString];
    
    if (!_todaySport || ![_todaySport.dateString isEqualToString:dateStr]) {
        _todaySport = [SportModel getSportModelFromDB];
    }
    return _todaySport;
}

- (SportModel *)yesSport {
    NSString *dateStr = [[[NSDate date] dateAfterDay:-1] dateToDayString];
    
    if (!_yesSport || ![_yesSport.dateString isEqualToString:dateStr]) {
        _yesSport = [SportModel getSportModelFromDBWithDayString:dateStr];
    }
    return _yesSport;
}

// 今日与昨天的睡眠参数
- (SleepModel *)todaySleep {
    NSString *dateStr = [[NSDate date] dateToDayString];
    
    if (!_todaySleep || ![_todaySleep.dateString isEqualToString:dateStr]) {
        _todaySleep = [SleepModel getSleepModelFromDB];
    }
    return _todaySleep;
}

- (SleepModel *)yesSleep {
    NSString *dateStr = [[[NSDate date] dateAfterDay:-1] dateToDayString];
    
    if (!_yesSleep || ![_yesSleep.dateString isEqualToString:dateStr]) {
        _yesSleep = [SleepModel getSleepModelFromDBWithDayString:dateStr];
    }
    return _yesSleep;
}

// 今日与昨天的心率参数
- (HeartModel *)todayHeart {
    NSString *dateStr = [[NSDate date] dateToDayString];
    
    if (!_todayHeart || ![_todayHeart.dateString isEqualToString:dateStr]) {
        _todayHeart = [HeartModel getHeartModelFromDB];
    }
    return _todayHeart;
}

- (HeartModel *)yesHeart {
    NSString *dateStr = [[[NSDate date] dateAfterDay:-1] dateToDayString];
    
    if (!_yesHeart || ![_yesHeart.dateString isEqualToString:dateStr]) {
        _yesHeart = [HeartModel getHeartModelFromDBWithDayString:dateStr];
    }
    return _yesHeart;
}

// 今日与昨天的血氧
- (OxModel *)todayOx {
    NSString *dateStr = [[NSDate date] dateToDayString];
    
    if (!_todayOx || ![_todayOx.dateString isEqualToString:dateStr]) {
        _todayOx = [OxModel getOxModelFromDB];
    }
    return _todayOx;
}

- (OxModel *)yesOx {
    NSString *dateStr = [[[NSDate date] dateAfterDay:-1] dateToDayString];
    
    if (!_yesOx || ![_yesOx.dateString isEqualToString:dateStr]) {
        _yesOx = [OxModel getOxModelFromDBWithDayString:dateStr];
    }
    return _yesOx;
}

// 今日与昨天的血压
- (BpModel *)todayBp {
    NSString *dateStr = [[NSDate date] dateToDayString];
    
    if (!_todayBp || ![_todayBp.dateString isEqualToString:dateStr]) {
        _todayBp = [BpModel getBpModelFromDB];
    }
    return _todayBp;
}

- (BpModel *)yesBp {
    NSString *dateStr = [[[NSDate date] dateAfterDay:-1] dateToDayString];
    
    if (!_yesBp || ![_yesBp.dateString isEqualToString:dateStr]) {
        _yesBp = [BpModel getBpModelFromDBWithDayString:dateStr];
    }
    return _yesBp;
}

- (void)setStravaCode:(NSString *)stravaCode
{
    _stravaCode = stravaCode;
    [@"stravaCode" setObjectValue:stravaCode];
}

- (NSString*)showStravaCode
{
    NSString *sss = [@"stravaCode" getObjectValue];
    return sss;
}

- (void)setStravaToken:(NSString *)stravaToken
{
    _stravaToken = stravaToken;
    [@"stravaToken" setObjectValue:stravaToken];
}

-(NSString *)showStravaToken
{
    NSString *sss = [@"stravaToken" getObjectValue];
    NSString *token;
    if (sss) {
     token = [NSString stringWithFormat:@"Bearer %@",sss];
    }
    else
    {
        token = sss;
    }
    return token;
}

- (void)cleanCacheAndCookie
{
    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies]){
        [storage deleteCookie:cookie];
    }
    
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    NSURLCache * cache = [NSURLCache sharedURLCache];
    [cache removeAllCachedResponses];
    [cache setDiskCapacity:0];
    [cache setMemoryCapacity:0];
}

- (NSInteger)getFoodBmr
{
    NSInteger bmr = 0;
    if (SN_USER.gender ==1) {
        bmr = 10*SN_USER.weight +6.25*SN_USER.height - 5*SN_USER.age +5;
    }
    else
    {
        bmr = 10*SN_USER.weight +6.25*SN_USER.height - 5*SN_USER.age -161;
    }
      bmr = bmr/100*100;
    return bmr;
}

- (BOOL)isBoind
{
    BOOL state = NO;
    if ([KK_BLELastDevice getObjectValue] || [KK_BLELastDeviceMac getObjectValue]) {
        state = YES;
    }
    
    return state;
}

@end

